var express = require('express');
var router = express.Router();
const SchedulesApi = require("../api/schedules.js");

/* GET home page. */
router.post('/', async function (req, res, next) {
    const schedule = req.body;
    const data = await SchedulesApi.add(schedule);
    res.send(data);
});

router.get("/all", async function (req, res, next) {
    const data = await SchedulesApi.getAll()
    res.send(data);
})

router.get('/', async function (req, res, next) {
    const { page, limit } = req.query;
    const result = await SchedulesApi.get({ page, limit });
    res.send({
        "code": 0,
        "msg": "",
        "count": result.total,
        "data": result.rows,
    });
});

router.put('/:_id', async function (req, res, next) {
    const schedule = req.body;
    const _id = req.params;
    const data = await SchedulesApi.updata({ _id }, schedule);
    res.send(data);
})

router.delete('/:_id', async function (req, res, next) {
    const _id = req.params;
    const data = await SchedulesApi.delete(_id);
    res.send(data);
})

router.get('/:_id', async function (req, res, next) {
    const { _id } = req.params;
    const data = await SchedulesApi.getOne(_id);
    res.send(data);
});

module.exports = router;
